﻿import mx.utils.Delegate;
class cn.eminia.RClassV3.SliderV2_BoE {
	//主
	static var HORIZONTAL:String = "-";
	static var VERTICAL:String = "|";
	private var _handle:MovieClip;
	private var _back:MovieClip;
	private var _backReal:MovieClip;
	//次
	private var _type:String;
	//内
	private var _max:Number = 100;
	private var _min:Number = 0;
	private var _orientation:String;
	var lis, per;
	//
	function SliderV2_BoE(orientation:String, obj:Object) {
		//  
		_orientation = orientation;
		applyMC(obj);
	}
	function updatePos(prop, oldVal, newVal) {
		//this.rBar.y = (this.p0-newVal)/this._s+this.a0;
		if (_orientation == HORIZONTAL) {
			_handle._x = newVal*(_back._width-_handle._width)+_back._x;
		} else {
			_handle._y = newVal*(_back._height-_handle._height)+_back._y;
		}
	}
	function applyMC(obj) {
		_handle = obj.bar;
		if (obj.axis.real != null) {
			_back = obj.axis;
			_backReal = obj.axis.real;
		} else {
			_back = obj.axis;
		}
		_type = obj.type;
		if (_orientation == HORIZONTAL) {
			_min = _back._x;
			_max = _back._x+_back._width-_handle._width;
		} else {
			_min = _back._y;
			_max = _back._y+_back._height-_handle._height;
		}
		//
		new AS2.BoEV4_Drag(_handle, Delegate.create(this, function () {
			onDrag();
			lisPPP();
		}), {out:Delegate.create(this, function () {
			onDrop();
			killPPP();
		})});
		//_handle.onPress = 
		//_handle.onReleaseOutside = _handle.onRelease=
		//
		_back.onRollOver = Delegate.create(this, function () {
		});
		_back.onPress = Delegate.create(this, function () {
			if (_type == 'step') {
				FollowAxisStep();
			} else {
				FollowAxis();
			}
		});
	}
	function lisPPP() {
		var d = _root.getNextHighestDepth();
		lis = _root.createEmptyMovieClip('mc'+d, d);
		//
		lis.onEnterFrame = Delegate.create(this, function () {
			if (_orientation == HORIZONTAL) {
				per = (_handle._x-_back._x)/(_back._width-_handle._width);
				//_root.tf.text 
			} else {
				per = (_handle._y-_back._y)/(_back._height-_handle._height);
			}
		});
	}
	function killPPP() {
		removeMovieClip(lis);
	}
	///////////////////////////////////
	// public methods
	///////////////////////////////////  
	private function barRange(n:Number):Number {
		n = Math.min(n, _max);
		n = Math.max(n, _min);
		return n;
	}
	function FollowAxisStep() {
		//
		if (_orientation == HORIZONTAL) {
			var tar = this.barRange(_handle._parent._xmouse-_handle._width*.4);
			var dir = tar-_handle._x;
		} else {
			var tar = this.barRange(_handle._parent._ymouse-_handle._height*.4);
			var dir = tar-_handle._y;
		}
		//
		var d = _root.getNextHighestDepth();
		lis = _root.createEmptyMovieClip('mc'+d, d);
		lis.onEnterFrame = Delegate.create(this, function () {
			if (_orientation == HORIZONTAL) {
				if (dir>0) {
					_handle._x = Math.min(_handle._x+5, tar);
				} else {
					_handle._x = Math.max(_handle._x-5, tar);
				}
				per = (_handle._x-_back._x)/(_back._width-_handle._width);
				//
				if (_handle._x == tar) {
					var doEnd = true;
				}
			} else {
				if (dir>0) {
					_handle._y = Math.min(_handle._y+5, tar);
				} else {
					_handle._y = Math.max(_handle._y-5, tar);
				}
				per = (_handle._y-_back._y)/(_back._height-_handle._height);
				//
				if (_handle._y == tar) {
					var doEnd = true;
				}
			}
			//
			if (doEnd) {
				killPPP();
				//trace(_handle.hitTest(_root._xmouse, _root._ymouse));
				if (_handle.hitTest(_root._xmouse, _root._ymouse)) {
					trace('1 time');
					_handle.onPress();
					var axis_Lis = {};
					axis_Lis.onMouseUp = Delegate.create(this, function () {
						_handle.stopDrag();
						killPPP();
						Mouse.removeListener(axis_Lis);
					});
					Mouse.addListener(axis_Lis);
				}
			}
		});
		//lisPPP();
		//
		//_handle.onPress();
		//
		var axis_Lis = {};
		axis_Lis.onMouseUp = Delegate.create(this, function () {
			killPPP();
			Mouse.removeListener(axis_Lis);
		});
		Mouse.addListener(axis_Lis);
	}
	function FollowAxis() {
		if (_orientation == HORIZONTAL) {
			_handle._x = this.barRange(_handle._parent._xmouse-_handle._width*.4);
		} else {
			_handle._y = this.barRange(_handle._parent._ymouse-_handle._height*.4);
		}
		lisPPP();
		//
		_handle.onPress();
		//
		var axis_Lis = {};
		axis_Lis.onMouseUp = Delegate.create(this, function () {
			_handle.stopDrag();
			Mouse.removeListener(axis_Lis);
		});
		Mouse.addListener(axis_Lis);
	}
	///////////////////////////////////
	// event handlers
	/////////////////////////////////// 
	function onDrag(evt) {
		if (_orientation == HORIZONTAL) {
			_handle.startDrag(false, _back._x, _handle._y, _back._x+_back._width-_handle._width, _handle._y);
			//_root.tf.text = _handle;
		} else {
			_handle.startDrag(false, _handle._x, _back._y, _handle._x, _back._y+_back._height-_handle._height);
			//_root.tf.text = _handle;
		}
	}
	function onDrop(evt) {
		_handle.stopDrag();
	}
}
